#include <stdio.h>
#include <string.h>

int main()
{
    char wdata[] = "linux easy!";
    size_t len = strlen(wdata);
    //1.打开/创建文件
    FILE *fd = fopen("./bite.txt","w+");
    if(fd == NULL)
    {
        perror("fail:fd");
        return 1; 
    }
    
    //2.写入数据
    size_t written = fwrite(wdata, 1, len, fd);
    if(written != len)
    {
        perror("fwrite failed");
        fclose(fd);
        return 1;
    }
    //3.重置文件指针到开头
    fseek(fd, 0, SEEK_SET);
    printf("fseek!!!");

    //4.读取数据
    char rdata[100] = {0};
    size_t read = fread(rdata, 1, len, fd);
    if(read != len)
    {
        perror("fread failed");
        fclose(fd);
        return 1;
    }

    //5.打印读取的内容
    printf("File constent: %s\n", rdata);

    //6.关闭文件
    fclose(fd);
    return 0;
}

